package io.alterac.blurkit;

import com.enrique.stackblur.StackBlurManager;
import ohos.agp.components.Image;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.PixelMap;

/**
 * 描述 BlurKit
 *
 * @since 2021-05-06
 */
public class BlurKit {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
    private static BlurKit instance = null;
    private static final int NUM_100 = 100;
    private static final int NUM_25 = 25;

    /**
     * 构造参数不对外
     */
    private BlurKit() {
    }

    /**
     * 单例获取实例
     *
     * @param context
     */
    public static void init(Context context) {
        if (instance != null) {
            return;
        }

        instance = new BlurKit();
    }

    /**
     * 模糊化任何一个pixmap
     *
     * @param src pixmap
     * @return pixmap
     */
    public PixelMap blur(PixelMap src) {
        StackBlurManager stackBlurManager = new StackBlurManager(src);
        return stackBlurManager.process(NUM_25);
    }

    /**
     * 模糊化任何一个image(由于鸿蒙不能拿到ability的pixmap 故只能模糊Image)
     *
     * @param image Image
     * @return PixelMap
     */
    public PixelMap blur(Image image) {
        StackBlurManager stackBlurManager = new StackBlurManager(image.getPixelMap());
        return stackBlurManager.process(NUM_25);
    }

    /**
     * 模糊化image 并设置模糊度（0-100）
     *
     * @param image image
     * @param downscaleFactor 模糊度
     * @return PixelMap
     */
    public PixelMap fastBlur(Image image, float downscaleFactor) {
        StackBlurManager stackBlurManager = new StackBlurManager(image.getPixelMap());
        return stackBlurManager.process((int) (downscaleFactor * NUM_100));
    }

    /**
     * getInstance
     *
     * @return BlurKit对象
     */
    public static BlurKit getInstance() {
        if (instance == null) {
            instance = new BlurKit();
        }
        return instance;
    }
}
